package cn.sxw.ld.module.order.controller;

import cn.sxw.ld.common.dto.R;
import cn.sxw.ld.module.order.pojo.OrderDetail;
import cn.sxw.ld.module.order.pojo.Orders;
import cn.sxw.ld.module.shoppingcat.pojo.ShoppingCart;
import cn.sxw.ld.module.order.service.OrderDetailService;
import cn.sxw.ld.module.order.service.OrdersService;
import cn.sxw.ld.module.shoppingcat.service.ShoppingCartService;
import cn.sxw.ld.common.utils.BaseContext;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

/**
 * ClassName:OrdersController
 * Package:cn.sjxy.graduation.controller
 * Description:
 *
 * @Date:2022/6/26 13:54
 * @Author:SXW
 */
@Slf4j
@RestController
@RequestMapping("/order")
public class OrdersController {

    @Autowired
    private OrdersService ordersService;

    @Autowired
    private OrderDetailService orderDetailService;

    @Autowired
    private ShoppingCartService shoppingCartService;

    /**
     * 服务端：获取分页信息，展示数据
     *
     * @param page
     * @param pageSize
     * @return
     */
    @GetMapping("/page")
    public R<Page> page(int page, int pageSize, String number, String beginTime, String endTime) {
        log.info("page = {},pageSize = {}, number = {}", page, pageSize, number);
        Page ordersPage = ordersService.getPage(page, pageSize, number, beginTime, endTime);
        return R.success(ordersPage);
    }

    /**
     * 展示订单数据
     *
     * @param page
     * @param pageSize
     * @return
     */
    @GetMapping("/userPage")
    public R<Page> orderDetail(Integer page, Integer pageSize) {
        Page ordersDtoPage = ordersService.getUserPage(page, pageSize);
        return R.success(ordersDtoPage);
    }

    /**
     * 实现用户下单功能
     *
     * @param orders
     * @return
     */
    @PostMapping("/submit")
    public R<String> submit(@RequestBody Orders orders) {
        ordersService.submitOrder(orders);
        return R.success("下单成功！");
    }

    /**
     * 服务端修改配送状态
     *
     * @param orders
     * @return
     */
    @PutMapping
    public R<String> updateStatus(@RequestBody Orders orders) {
        LambdaQueryWrapper<Orders> ordersLambdaQueryWrapper = new LambdaQueryWrapper<>();
        ordersLambdaQueryWrapper.eq(Orders::getId, orders.getId());
        Orders one = ordersService.getOne(ordersLambdaQueryWrapper);
        one.setStatus(orders.getStatus());
        ordersService.updateById(one);
        return R.success("状态修改成功！");
    }

    /**
     * 再来一单，回传订单编号
     * 通过回传的订单号查询订单明细表获取菜品信息，获取部分信息
     * 将获取的菜品信息添加到购物车中
     * 只帮忙添加到购物车，并不会提价订单
     *
     * @param orders
     * @return
     */
    @PostMapping("/again")
    public R<String> again(@RequestBody Orders orders) {
        Long id = orders.getId();
        LambdaQueryWrapper<OrderDetail> orderDetailLambdaQueryWrapper = new LambdaQueryWrapper<>();
        orderDetailLambdaQueryWrapper.eq(OrderDetail::getOrderId, id);
        // 一个订单内有多个商品，一个订单也就对应着多条记录
        List<OrderDetail> orderDetails = orderDetailService.list(orderDetailLambdaQueryWrapper);


        // 遍历处理菜品与套餐之间特殊的信息
        orderDetails.stream().map(item -> {
            // 为每一个商品创建一个购物车信息
            ShoppingCart cart = new ShoppingCart();
            // 获取菜品
            Long dishId = item.getDishId();
            if (dishId != null) { // 说明本单是菜品
                cart.setDishId(item.getDishId());
                // 获取口味信息
                cart.setDishFlavor(item.getDishFlavor());
            } else { // 说明本单是套餐
                cart.setSetmealId(item.getSetmealId());
            }

            // 设置公共信息，口味、数量、金额等
            cart.setName(item.getName());
            cart.setUserId(BaseContext.getCurrentId());
            cart.setAmount(item.getAmount());
            cart.setNumber(item.getNumber());
            cart.setImage(item.getImage());
            cart.setCreateTime(LocalDateTime.now());
            // 处理结束，添加到购物车
            shoppingCartService.add(cart);
            return null;
        }).collect(Collectors.toList());


        return R.success("已添加到购物车~");
    }
}
